﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FrameWork.UI.UserControls
{
    /// <summary>
    /// TDateTimeView.xaml 的交互逻辑
    /// </summary>
    public partial class TDateTimeView : UserControl
    {
        string formerDateTimeStr { get;   set; }

        public TDateTimeView()
        {
            InitializeComponent();
        }

        public TDateTimeView(string txt)
        {
            InitializeComponent();
            this.formerDateTimeStr = txt;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            textBlockhh.Text = "00";
            textBlockmm.Text = "00";
            textBlockss.Text = "00";
        }


        private void ibtnCloseView_Click(object sender, RoutedEventArgs e)
        {
           
            OnDateTimeContent(formerDateTimeStr);
       
        }
        /// <summary>
       /// 时间确定后的传递事件
       /// </summary>
       public Action<string> DateTimeOK;
 
       /// <summary>
       /// 时间确定后传递的时间内容
       /// </summary>
       /// <param name="dateTimeStr"></param>
       protected void OnDateTimeContent(string dateTimeStr)
       {
           if (DateTimeOK != null)
               DateTimeOK(dateTimeStr);
       }
        private void calDate_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void textBlockhh_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (popupChoice.IsOpen==true)
            {
                popupChoice.IsOpen = false;
            }
            THourView hourView = new THourView(textBlockhh.Text);
            hourView.HourClick += (hourstr) =>
            {
                textBlockhh.Text= hourstr;
                popupChoice.IsOpen = false;
            };
            popupChoice.Child = hourView;
            popupChoice.IsOpen = true;
        }

        private void textBlockmm_MouseUp(object sender, MouseButtonEventArgs e)
        {
            TMinSecView minView = new TMinSecView(textBlockmm.Text);//TMinSexView 构造函数传递 分钟数据
            minView.MinClick += (minStr) => //TMinSexView 中 点击选择的分钟数据的 传递动作
            {

                textBlockmm.Text = minStr;
                popupChoice.IsOpen = false;//TMinSexView 所在的 pop 关闭动作
            };

            popupChoice.Child = minView;
            popupChoice.IsOpen = true;
        }

        private void textBlockss_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (popupChoice.IsOpen == true)
            {
                popupChoice.IsOpen = false;
            }

            //秒钟 跟分钟 都是60，所有秒钟共用 分钟的窗体即可
            TMinSecView sexView = new TMinSecView(textBlockss.Text);//TMinSexView 构造函数 传入秒钟数据
            sexView.textBlockTitle.Text = "秒    钟";//修改 TMinSexView 的标题名称为秒钟
            sexView.MinClick += (sexStr) => //TMinSexView 中 所选择确定的 秒钟数据 的传递动作
            {
                textBlockss.Text = sexStr;
                popupChoice.IsOpen = false;//TMinSexView 所在的 pop 关闭动作
            };

            popupChoice.Child = sexView;
            popupChoice.IsOpen = true;
        }

        private void btnNow_Click(object sender, RoutedEventArgs e)
        {
            popupChoice.IsOpen = false;
            if (btnNow.Content.ToString()=="零点")
            {
                textBlockhh.Text = "00";
                textBlockmm.Text = "00";
                textBlockss.Text = "00";
                btnNow.Content = "当前";
                btnNow.Background = System.Windows.Media.Brushes.LightBlue;

            }
            else
            {
                DateTime dt = DateTime.Now;
                textBlockhh.Text = dt.Hour.ToString().PadLeft(2, '0');
                textBlockmm.Text = dt.Minute.ToString().PadLeft(2, '0');
                textBlockss.Text = dt.Second.ToString().PadLeft(2, '0');
                btnNow.Content = "零点";
                btnNow.Background = System.Windows.Media.Brushes.LightGreen;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            DateTime? dt = null;
            //日历
            if (calDate.SelectedDate==null)
            {
                dt = DateTime.Now.Date;

            }
            else
            {
                dt = calDate.SelectedDate;

            }

            DateTime dtCAl=Convert.ToDateTime(dt);
            string dateStr = dtCAl.ToString("yyyy/MM/dd");

            string timeStr = "00:00:00";
            timeStr=textBlockhh.Text+":"+textBlockmm.Text+":"+textBlockss.Text;

            string dataTimeStr = dateStr + " " + timeStr;
            OnDateTimeContent(dataTimeStr);
        }
    }
}
